package com.lepu.app.fun.my.adapter;

import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.MyNetUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.DateUtilBase;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.bean.BeanCollectKnowledge;

public class AdapterCollectKnowledge extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanCollectKnowledge> mArrayList = null;

	public AdapterCollectKnowledge(Context context, ArrayList<BeanCollectKnowledge> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(R.layout.my_collect_knowledge_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView.findViewById(R.id.mainItemLayout);
			ImageView iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
			TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView);
			TextView timeTextView = (TextView) convertView.findViewById(R.id.timeTextView);

			holder.mainItemLayout = mainItemLayout;
			holder.iconImageView = iconImageView;
			holder.titleTextView = titleTextView;
			holder.timeTextView = timeTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final BeanCollectKnowledge item = mArrayList.get(position);

		holder.titleTextView.setText(item.Title);
		if(item.Time > 0)
		{
			holder.timeTextView.setVisibility(View.VISIBLE);

			String dateString = DateUtilBase.stringFromDate(new Date(item.Time), DateUtilBase.DATE_ALL_ALL);
			holder.timeTextView.setText(DateUtilBase.friendly_time(dateString));
		}else{
			holder.timeTextView.setVisibility(View.GONE);
		}
		holder.mainItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                MyNetUtil.getInstance().requestArticleDetail(
                        (BaseFragmentActivity)mContext,
                        item.ArticleID,"","","");
            }
        });
        holder.iconImageView.setVisibility(View.GONE);
		return convertView;
	}

	private static class ViewHolder {
		LinearLayout mainItemLayout;
		ImageView iconImageView;
		TextView titleTextView;
		TextView timeTextView;
	}
}
